package com.t.bricks.bussiness.db.dao.examination.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.common.CommonDao;
import com.t.bricks.bussiness.db.dao.examination.ExaminationDao;
import com.t.bricks.bussiness.db.entity.examination.Examination;
import com.t.bricks.bussiness.db.entity.examination.ExaminationAnswer;
import com.t.bricks.model.MsgEmity;

@Repository
public class ExaminationDaoImpl implements ExaminationDao<Examination> {

	@Autowired
	private CommonDao commonDao;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 添加
	 * @param examination
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity addCustom(Examination examination) {
		MsgEmity me = this.add(examination);
		if (!me.isSuccess()) {
			return me;
		}
		
		if (null == examination.getAnswerList() || examination.getAnswerList().size() < 1) {
			return new MsgEmity(true, "新增成功！", examination);
		}
		
		me = commonDao.adds(examination.getAnswerList());
		if (!me.isSuccess()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return me;
		}
		
		return new MsgEmity(true, "新增成功！", examination);
	}

	/**
	 * 删除
	 * @param sId 记录编号
	 * @param iVersion 数据版本号
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity delCustom(String sId, Integer iVersion) {
		MsgEmity me = this.del(sId, iVersion);
		if (!me.isSuccess()) {
			return me;
		}
		
		me = commonDao.delByKey("sExaminationId", sId, null, false, ExaminationAnswer.class);
		if (!me.isSuccess()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return me;
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

}
